<?php

namespace app\user\controller;

class Tenderapply extends base
{
//前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
        'chackUser' => ['only' => 'index,edit'],
    ];

    protected function chackUser()
    {
        if (empty($this->getLoginUser()->status)) {
            $this->error(lang('Not open this feature'));
        }
    }

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();

            if (isset($data['status']) || !empty($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }

            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
        $this->datas ['uid'] = $this->getLoginUser()->id;
    }



    public function Tendersave(){
        //保存应标信息
        if(!$this->getLoginUser()){
            return ajax_info(lang('Please login or register'),'y',url('user/index/user'));
        }
        $data = $this->request->post();
        $data['uid'] = $this->getLoginUser()->id;
        $dd = $this->model->isUpdate(true)->save(['status' => '0','center'=>$data['center'],'money'=>$data['money']], ['tid' => $data['tid'], 'uid' => $data['uid']]);
        if ($dd) {
            return ajax_info(lang('Has re-submitted tender information'), 'u');
        }
        $data['lang']=cookie('think_var');

        $res = $this->model->isUpdate(false)->allowField(true)->save($data);
        if ($res) {
            return ajax_info(lang('Tender information has been submitted'), 'u');
        } else {
            return ajax_info(lang('Failed to submit data'));
        }
    }

    /**
     * 读取数据
     */
    public function read(){
        /*列表start*/
        $data = db('tender')->alias('T')
            ->join('__TENDERAPPLY__ TA', 'TA.tid= T.id')
            ->field('T.*,TA.center,TA.money')
            ->where('TA.uid', $this->getLoginUser()->id)
            ->find();
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch();
    }


}
